package CanvaWeb.CanvaWeb.Repository;

import CanvaWeb.CanvaWeb.pojo.Word;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface WordRepository extends JpaRepository<Word, Long> {

    @Query(value = "SELECT * FROM words ORDER BY RAND() LIMIT ?1", nativeQuery = true)
    List<Word> findRandomWords(int limit);

    // 添加一个方法，获取一个随机的词语
    default Word getRandomWord() {
        List<Word> words = findRandomWords(1);
        return words.isEmpty() ? null : words.get(0); // 返回随机的第一个词语，若没有则返回null
    }
}

